#include "StdAfx.h"
#include "Plane.h"
#include "Ray.h"
#include "Point3D.h"

CPlane::CPlane()
{

}
CPlane::CPlane(const CPoint3D& _Point, const CPoint3D& _Normal) : CPrimitive()
, Point(_Point)
, Normal(_Normal)
{
	Normal.Normalize();
}

CPlane::~CPlane(void)
{
}
void CPlane::SetPoint(CPoint3D& point)
{
	Point = point;
}
void CPlane::SetNormal(CPoint3D& normal)
{
	Normal = normal;
}
CPoint3D& CPlane::GetPoint()
{
	return Point;
}
CPoint3D& CPlane::GetNormal()
{
	return Normal;
}
float CPlane::Intersect(CRay& ray, CPoint3D& intersection, CPoint3D& normal)
{
	//if (Normal.Dot(ray.GetDirection()) > 0.f) return -1.f;

	float teller = Normal.Dot(Point - ray.GetOrigin());
	float noemer = Normal.Dot(ray.GetDirection());
	if (fabs(noemer) < EPSILON) return -1.f;
	
	float t = (teller/noemer);
	if (t < 0) return -1.f;

	intersection = ray.GetOrigin() + ray.GetDirection() * t;

	normal = Normal;
	return t;
}